/**
 * 打印工具
 */
import {
  PrinterManager
} from "@/utils/PrinterManager.js"
export class PrinterUtils {
  constructor() {
    this.printerManager = new PrinterManager();
  }

  //打印测试
  async printTest() {
    //初始化打印机
    await this.printerManager.initPrinter();
    await this.printerManager.printText("打印文本效果展示：")
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 2)

    await this.printerManager.setFont(0, 0, 0, 0, 0)
    await this.printerManager.setPrinter("ALIGN", 0)
    await this.printerManager.printText("文字居左效果演示abc123：")
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 2)

    await this.printerManager.setPrinter("ALIGN", 1)
    await this.printerManager.printText("文字居中效果演示abc123：")
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 2)

    await this.printerManager.setPrinter("ALIGN", 2)
    await this.printerManager.printText("文字居右效果演示abc123：")
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 3)

    await this.printerManager.setPrinter("ALIGN", 0)
    await this.printerManager.setFont(0, 0, 0, 1, 0)
    await this.printerManager.printText("文字加粗效果演示abc123：")
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 2)

    await this.printerManager.setFont(0, 0, 0, 0, 1)
    await this.printerManager.sendBytesData([0x1C, 0x21, 0x80]);
    await this.printerManager.printText("文字下划线效果演示：")
    await this.printerManager.sendBytesData([0x1C, 0x21, 0x00]);
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 2)

    await this.printerManager.setFont(0, 0, 0, 0, 0)
    await this.printerManager.printText("同行不同高效果演示：")
    for (let i = 0; i < 4; i++) {
      await this.printerManager.setFont(0, i, i, 0, 0)
      await this.printerManager.printText((i + 1) + "倍")
    }

    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 1)
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 3)

    for (let i = 0; i < 4; i++) {
      await this.printerManager.setFont(0, i, i, 0, 0)
      await this.printerManager.printText("放大" + (i + 1) + "倍效果演示abc123")
      await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 3)
    }

    await this.printerManager.setFont(0, 0, 0, 0, 0)
    await this.printerManager.setPrinter("ALIGN", 0)
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 3)

    //打印完成后切纸
    await this.printerManager.cutPaper(false)
  }

  //小票打印
  async printNote() {
    await this.printerManager.initPrinter();
    await this.printerManager.setFont(0, 0, 0, 0, 0)
    await this.printerManager.setPrinter("ALIGN", 0)
    await this.printerManager.printText("打印小票效果演示:")
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 2)

    await this.printerManager.setPrinter("ALIGN", 1)
    await this.printerManager.setFont(0, 1, 1, 0, 0)
    await this.printerManager.printText("杭州xxx超市\n")
    await this.printerManager.setPrinter("ALIGN", 0)

    //字号使用默认
    await this.printerManager.setFont(0, 0, 0, 0, 0)
    let str = "门店号：574001\n";
    str += "单据：S00003169\n";
    str += "收银员：s004_s004\n";
    str += "单据日期：2025-02-28\n";
    str += "打印时间：2025-02-28 09:53:19\n";
    await this.printerManager.printText(str)

    //打印表格
    await this.printTable()

    //获取纸张的宽度
    let res = await this.printerManager.getPaperWidth();
    console.log(res)
    let width = 384;
    if (res.code == 0) {
      let data = res.data || {};
      width = data.width || 384;
    }

    let str1 = "";
    if (width == 384) {
      str1 += "数量：                    6.00\n";
      str1 += "总计：                    35.00\n";
      str1 += "付款：                    100.00\n";
      str1 += "找零：                    65.00\n";
    } else if (width == 576) {
      str1 += "数量：                                6.00\n";
      str1 += "总计：                                35.00\n";
      str1 += "付款：                                100.00\n";
      str1 += "找零：                                65.00\n";
    } else if (width == 724) {
      str1 += "数量：                                            6.00\n";
      str1 += "总计：                                            35.00\n";
      str1 += "付款：                                            100.00\n";
      str1 += "找零：                                            65.00\n";
    }
    str1 += "公司名称：杭州某某家居超市\n";
    str1 += "公司网址：www.jiangsuxxxx.com\n";
    str1 += "地址：杭州市某某路某某号\n";
    str1 += "电话：0574-12345678\n";
    str1 += "服务专线：4008-123-456\n";

    if (width == 384) {
      str1 += "==============================\n";
    } else if (width == 576) {
      str1 += "==============================================\n";
    } else if (width == 724) {
      str1 += "================================================================\n";
    }
    await this.printerManager.printText(str1)

    await this.printerManager.setPrinter("ALIGN", 1)
    await this.printerManager.setFont(0, 0, 1, 0, 0)
    await this.printerManager.printText("谢谢惠顾,欢迎再次光临！\n")
    await this.printerManager.printText("Demo 版本！\n\n\n")

    await this.printerManager.setFont(0, 0, 0, 0, 0)
    await this.printerManager.setPrinter("ALIGN", 0)
    await this.printerManager.setPrinter("PRINT_AND_WAKE_PAPER_BY_LINE", 3)

    //打印完成后切纸
    await this.printerManager.cutPaper(false)
  }

  //打印表格
  async printTable() {
    let column = "品名;数量;单价;金额";
    //获取纸张宽度
    //获取纸张的宽度
    let res = await this.printerManager.getPaperWidth();
    let width = 384;
    let columnWidth = [14, 6, 6, 6];
    if (res.code == 0) {
      let data = res.data || {};
      width = data.width || 384;
    }

    if (width == 384) {
      columnWidth = [14, 6, 6, 6];
    } else if (width == 576) {
      columnWidth = [18, 10, 10, 12];
    } else if (width == 724) {
      columnWidth = [22, 14, 14, 18];
    }

    let rows = [
      "保鲜袋;1;10.00;10.00",
      "铁丝挂钩;2;5.00;10.00",
      "雨伞;3;5.00;15.00"
    ];
    await this.printerManager.printTable(column, ";", columnWidth, rows);
  }
}